package cn.iocoder.yudao.module.jifenyuan.service.walletlog;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.jifenyuan.controller.app.walletlog.vo.*;
import cn.iocoder.yudao.module.jifenyuan.dal.dataobject.walletlog.WalletLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.jifenyuan.convert.walletlog.WalletLogConvert;
import cn.iocoder.yudao.module.jifenyuan.dal.mysql.walletlog.WalletLogMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.jifenyuan.enums.ErrorCodeConstants.*;

/**
 * 记分员 钱包记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WalletLogServiceImpl implements WalletLogService {

    @Resource
    private WalletLogMapper walletLogMapper;

    @Override
    public Long createWalletLog(AppWalletLogCreateReqVO createReqVO) {
        // 插入
        WalletLogDO walletLog = WalletLogConvert.INSTANCE.convert(createReqVO);
        walletLogMapper.insert(walletLog);
        // 返回
        return walletLog.getId();
    }

    @Override
    public void updateWalletLog(AppWalletLogUpdateReqVO updateReqVO) {
        // 校验存在
        validateWalletLogExists(updateReqVO.getId());
        // 更新
        WalletLogDO updateObj = WalletLogConvert.INSTANCE.convert(updateReqVO);
        walletLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteWalletLog(Long id) {
        // 校验存在
        validateWalletLogExists(id);
        // 删除
        walletLogMapper.deleteById(id);
    }

    private void validateWalletLogExists(Long id) {
        if (walletLogMapper.selectById(id) == null) {
            throw exception(WALLET_LOG_NOT_EXISTS);
        }
    }

    @Override
    public WalletLogDO getWalletLog(Long id) {
        return walletLogMapper.selectById(id);
    }

    @Override
    public List<WalletLogDO> getWalletLogList(Collection<Long> ids) {
        return walletLogMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<WalletLogDO> getWalletLogPage(AppWalletLogPageReqVO pageReqVO) {
        pageReqVO.setUserId(SecurityFrameworkUtils.getLoginUserId());
        return walletLogMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WalletLogDO> getWalletLogList(AppWalletLogExportReqVO exportReqVO) {
        return walletLogMapper.selectList(exportReqVO);
    }

}
